var express = require('express');
const axios = require('axios');

var router = express.Router();

let codes = {}  //保存验证码在内存中

router.get('/2', function (req, res, next) {
    // res.send("个人信息页面");
    res.render('app/register/reg2');
})

router.get('/1', function (req, res, next) {
    // res.send("个人信息页面");
    res.render('app/register/reg1');
})

/**
 * @api {post} /admin/register/sendMessage 根据手机号获取验证码
 * @apiName 用户注册
 * @apiGroup User
 *
 * @apiParam {String} phone 手机号.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/sendMessage', (req, res) => {
    let { phone } = req.body
    console.log(phone)

    var config = {
        method: 'post',
        baseURL: 'http://localhost:9022',
        url: '/tb-user/sendMessage',
        params: {
            phone: phone
        },
        headers: {//指定响应头
            "Content-Type": "application/json;charset=utf-8",
            "Accept": "application/json"
        }
    }
    axios(config)
        .then(function (response) {
            console.log(response.data);
            codes = {
                code: response.data.data
            }
            console.log("成功")
            console.log(codes)
            res.redirect('/admin/register/1');
        })
        .catch(function (error) {
            console.log(error);
            console.log("失败")
        });
})

/**
 * @api {post} /admin/register/isTrueCode 验证手机号
 * @apiName 用户注册
 * @apiGroup User
 *
 * @apiParam {String} phone 手机号.
 * @apiParam {String} code 验证码.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
// codes = {
//     code: 5789
// }
router.post('/isTrueCode', (req, res) => {
    let { phone, code } = req.body
    console.log(codes.code)
    console.log(phone + "-----" + code)
    if (codes.code != code) {
        return res.send({ err: -1, msg: '验证码错误' })
    } else {
        req.session.phone = phone
        console.log("vcsucc")
        res.send({ err: 0, msg: "成功" })
        res.redirect('/admin/register/2');
    }
})

/**
 * @api {post} /admin/register/ispassword 密码完成注册
 * @apiName 用户注册
 * @apiGroup User
 *
 * @apiParam {String} phone 手机号.
 * @apiParam {String} username 验证码.
 * @apiParam {String} password 密码.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/ispassword', (req, res) => {
    let { phone, username, password, passwordAgain } = req.body

    if (password === passwordAgain) {
        var config = {
            method: 'post',
            baseURL: 'http://localhost:9022',
            url: '/tb-user/ispassword',
            params: {
                phone: req.session.phone,
                username: username,
                password: password
            },
            headers: {//指定响应头
                "Content-Type": "application/json;charset=utf-8",
                "Accept": "application/json"
            }
        }
        axios(config)
            .then(function (response) {
                console.log(response.data);
                codes = {
                    code: response.data.data
                }
                console.log("成功")
                console.log(codes)
                res.send({ err: 0, msg: "成功" })
                res.redirect('/admin/login');
            })
            .catch(function (error) {
                console.log(error);
                console.log("失败")
            });
    } else {
        res.send({
            err: -1,
            msg: "两次密码输入不一致"
        })
    }
})

module.exports = router;